/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sales.managedbean;

import javax.faces.event.ActionEvent;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import sales.entity.LineItem;
import sales.entity.SalesInquiry;
import sales.seesion.stateless.SalesInquirySessionBean;
import sales.util.exception.SalesException;

/**
 *
 * @author frankxgz
 */
@ManagedBean
@ViewScoped
public class EditSalesInquiry implements Serializable{
    
    @EJB
    private SalesInquirySessionBean inquirySessionBean;
    private SalesInquiry inquiry;
    private Long[] productIds = {Long.valueOf(1L),Long.valueOf(2L),Long.valueOf(3L),Long.valueOf(4L)};
    private int quantity1 = 0;
    private int quantity2 = 0;
    private int quantity3 = 0;
    private int quantity4 = 0;
    private List<LineItem> lineItems;
    

    /** Creates a new instance of EditSalesInquiry */
    public EditSalesInquiry() {
    }
    
    public void initView(PhaseEvent event) {
        PhaseId phaseId = event.getPhaseId();
        if (phaseId.equals(PhaseId.RENDER_RESPONSE)){
            if (getInquiry() == null) {
                setInquiry((SalesInquiry) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("inquiry"));
            }
            for (int i=0;i<inquiry.getInquiryLineItems().size();i++){
                if (inquiry.getInquiryLineItems().get(i).getProduct().getId().equals(1L))
                    quantity1 = inquiry.getInquiryLineItems().get(i).getQuantity();
                if (inquiry.getInquiryLineItems().get(i).getProduct().getId().equals(2L))
                    quantity2 = inquiry.getInquiryLineItems().get(i).getQuantity();
                if (inquiry.getInquiryLineItems().get(i).getProduct().getId().equals(3L))
                    quantity3 = inquiry.getInquiryLineItems().get(i).getQuantity();
                if (inquiry.getInquiryLineItems().get(i).getProduct().getId().equals(4L))
                    quantity4 = inquiry.getInquiryLineItems().get(i).getQuantity();
            }
        }
    }
    
    public void saveInquiryChange(ActionEvent event) throws IOException, SalesException{
        List productsList = new ArrayList<Integer>();
        List quantitiesList = new ArrayList<Integer>();
        
        if (getQuantity1()!=0){ productsList.add(productIds[0]);quantitiesList.add(getQuantity1());}
        if (getQuantity2()!=0){ productsList.add(productIds[1]);quantitiesList.add(getQuantity2());}
        if (getQuantity3()!=0){ productsList.add(productIds[2]);quantitiesList.add(getQuantity3());}
        if (getQuantity4()!=0){ productsList.add(productIds[3]);quantitiesList.add(getQuantity4());}
        
        inquirySessionBean.deleteInquiryLineItems(inquiry.getId());
        inquiry = inquirySessionBean.saveSalesInquiry(inquiry.getId(),productsList,quantitiesList);
        
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newInquiry", inquiry);
        FacesContext.getCurrentInstance().getExternalContext().redirect("createSalesInquiryResult.xhtml");
    }
    
    public void inquiryCancel(ActionEvent event) throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newInquiry", inquiry);
        FacesContext.getCurrentInstance().getExternalContext().redirect("createSalesInquiryResult.xhtml");
    }

    public SalesInquirySessionBean getInquirySessionBean() {
        return inquirySessionBean;
    }

    public void setInquirySessionBean(SalesInquirySessionBean inquirySessionBean) {
        this.inquirySessionBean = inquirySessionBean;
    }

    public SalesInquiry getInquiry() {
        return inquiry;
    }

    public void setInquiry(SalesInquiry inquiry) {
        this.inquiry = inquiry;
    }

    public int getQuantity1() {
        return quantity1;
    }

    public void setQuantity1(int quantity1) {
        this.quantity1 = quantity1;
    }

    public int getQuantity2() {
        return quantity2;
    }

    public void setQuantity2(int quantity2) {
        this.quantity2 = quantity2;
    }

    public int getQuantity3() {
        return quantity3;
    }

    public void setQuantity3(int quantity3) {
        this.quantity3 = quantity3;
    }

    public int getQuantity4() {
        return quantity4;
    }

    public void setQuantity4(int quantity4) {
        this.quantity4 = quantity4;
    }
    
}
